home *** CD-ROM | disk | FTP | other *** search
/ Interactive Web Graphics with Shout 3D / Interactive Web Graphics With Shout 3D.iso / mac / Code / Chapter10 / MultiViewpointsGUIPanel.java < prev    next >
Text File  |  2000-09-08  |  4KB  |  158 lines

  1. package applets;
  2.  
  3. import shout3d.*;
  4. import shout3d.core.*;
  5. import custom_nodes.*;
  6.  
  7.  
  8. public class MultiViewpointsGUIPanel extends Shout3DPanel implements DeviceObserver {
  9.    
  10.    
  11.    int pixelStartX;
  12.    int pixelStartY;
  13.    
  14.    float headingSpeed = 0.0f;
  15.    float pitchSpeed = 0.0f;
  16.    float distanceSpeed = 0.0f;
  17.    float zoomPixelFactor = 0.0f;
  18.  
  19.    TargetViewpoint[] cameras = new TargetViewpoint [3];
  20.    int currentCam = 1;
  21.    int totalCams = 1;
  22.    
  23.    
  24.    float initialHeading;
  25.    float initialPitch;
  26.    float initialDistance;
  27.    float[] initialCenter;
  28.    float initialFieldOfView;   
  29.  
  30.  
  31.  
  32.    public MultiViewpointsGUIPanel(Shout3DApplet applet, int width, int height){
  33.       super(applet, width, height);
  34.    }      
  35.  
  36.  
  37.    public void customInitialize() {
  38.         
  39.       addDeviceObserver(this,"MouseInput", null);
  40.       getRenderer().addRenderObserver(this, null);
  41.       cameras[0] = (TargetViewpoint) getCurrentBindableNode("Viewpoint");
  42.       
  43.       //store initial values
  44.       initialHeading = cameras[0].heading.getValue(); 
  45.       initialPitch = cameras[0].pitch.getValue();
  46.       initialDistance = cameras[0].distance.getValue();
  47.       initialCenter = cameras[0].center.getValue();
  48.       initialFieldOfView = cameras[0].fieldOfView.getValue();
  49.       
  50.       zoomPixelFactor = (initialDistance/10)/150;     
  51.       
  52.    
  53.    }
  54.  
  55.  
  56.    protected void finalize()  { 
  57.       removeDeviceObserver(this,"MouseInput");
  58.       getRenderer().removeRenderObserver(this);
  59.    }
  60.  
  61.    
  62.    
  63.    public boolean onDeviceInput(DeviceInput di, Object userData) {
  64.      
  65.       MouseInput mi = (MouseInput) di;
  66.       switch (mi.which){
  67.  
  68.          case MouseInput.DOWN:
  69.             pixelStartX = mi.x;
  70.             pixelStartY = mi.y;
  71.             return true;
  72.          
  73.          case MouseInput.UP:
  74.             headingSpeed = 0.0f;
  75.             pitchSpeed = 0.0f;
  76.             distanceSpeed = 0.0f;
  77.             return true;
  78.  
  79.          case MouseInput.DRAG:
  80.  
  81.             //if left button used
  82.             if(mi.button == 0) {
  83.            
  84.                int pixelEndX = mi.x;
  85.                int pixelEndY = mi.y;
  86.  
  87.                int dragDistanceX = pixelEndX - pixelStartX;
  88.                int dragDistanceY = pixelEndY - pixelStartY;
  89.             
  90.                headingSpeed = dragDistanceX/70f;
  91.                pitchSpeed = dragDistanceY/70f;             
  92.       
  93.                return true;
  94.             }
  95.  
  96.               //if right button used
  97.               if(mi.button == 1) {
  98.  
  99.                int pixelEndY = mi.y;
  100.                int dragDistanceY = pixelEndY - pixelStartY;
  101.                distanceSpeed = dragDistanceY*zoomPixelFactor;
  102.                return true;
  103.             }
  104.       
  105.       }//end of switch
  106.  
  107.       return false;
  108.  
  109.    }//end of onDeviceInput()
  110.    
  111.    
  112.    
  113.    public boolean createCamera() {
  114.  
  115.         if (totalCams < 3) {
  116.                
  117.         totalCams++;
  118.         currentCam = totalCams;
  119.         cameras[currentCam-1] = new TargetViewpoint(this);
  120.    
  121.         //add to scene and bind
  122.         Node [] kids = {cameras[currentCam-1]};
  123.         getScene().addChildren(kids);
  124.                      
  125.         //set to initial values
  126.         cameras[currentCam-1].isBound.setValue(true);
  127.         cameras[currentCam-1].heading.setValue(initialHeading);
  128.         cameras[currentCam-1].pitch.setValue(initialPitch);
  129.         cameras[currentCam-1].distance.setValue(initialDistance);
  130.         cameras[currentCam-1].center.setValue(initialCenter);
  131.         cameras[currentCam-1].fieldOfView.setValue(initialFieldOfView);  
  132.                      
  133.         return true;
  134.      }         
  135.      return false;
  136.    }//end of createCamera()
  137.  
  138.    
  139.  
  140.    public void onPreRender (Renderer r, Object o) {
  141.       
  142.       float headingDelta = headingSpeed/getFramesPerSecond();
  143.       float temp = cameras[currentCam-1].heading.getValue() - headingDelta;
  144.       cameras[currentCam-1].heading.setValue(temp);
  145.  
  146.       float pitchDelta = pitchSpeed/getFramesPerSecond();      
  147.        temp = cameras[currentCam-1].pitch.getValue() - pitchDelta;
  148.       cameras[currentCam-1].pitch.setValue(temp);
  149.       
  150.       float distanceDelta = distanceSpeed/getFramesPerSecond();         
  151.       temp = cameras[currentCam-1].distance.getValue() + distanceDelta;
  152.       cameras[currentCam-1].distance.setValue(temp);
  153.    
  154.    }//end of onPreRender()
  155.    
  156.  
  157. }//end of class   
  158.